package com.linsen.core.mybatisplus.extension;

import cn.hutool.core.util.ObjectUtil;
import com.linsen.core.common.context.TokenContextHolder;
import com.linsen.core.common.enums.YnType;
import com.linsen.core.common.model.User;
import com.linsen.core.mybatisplus.constant.EntityConstant;
import com.linsen.core.redis.util.RedisSecurityUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 根据登录用户权限信息自动组装拼接SQL
 * @ProjectName: erp
 * @Package: com.linsen.core.mybatisplus.extension
 * @ClassName: SqlFilter
 * @Author: zhaoyong
 * @Email: zhaoy02@tsingyun.net
 * @Date: 2024/3/29 9:16
 * @Version: 1.0
 */
@Component
@RequiredArgsConstructor
public class SqlFilter {

    private final RedisSecurityUtil redisSecurityUtil;

    /**
     * 单表查询使用mybati-plus时使用apply()方法拼接自定义SQL
     * 如果涉及到多表查询且需要过滤数据权限时，从redis获取登录用户user信息后手动处理
     * @return
     */
    public String getPermissionSql(){
        User user = redisSecurityUtil.getUser(TokenContextHolder.getToken());
        Integer permissionType = user.getPermissionType();
        List<String> roleDataScopeDeptIdList = user.getRoleDataScopeDeptIdList();
        List<String> roleDataScopeUserIdList = user.getRoleDataScopeUserIdList();

        StringBuilder sqlFilter = new StringBuilder();
        if (ObjectUtil.equal(permissionType, YnType.NO.getValue())){
            List<String> sqlList = new ArrayList<>();

            if (ObjectUtil.isNotEmpty(roleDataScopeDeptIdList)){
                StringBuilder deptSql = new StringBuilder();

                if (roleDataScopeDeptIdList.size() == 1){
                    deptSql.append("generator_deptid").append(" = \"").append(roleDataScopeDeptIdList.get(0)).append("\"");
                }else{
                    deptSql.append("generator_deptid").append(" in(");
                    roleDataScopeDeptIdList = roleDataScopeDeptIdList.stream().map(deptId -> "\"" + deptId + "\"").collect(Collectors.toList());
                    deptSql.append(String.join(",",roleDataScopeDeptIdList)).append(")");
                }

                sqlList.add(deptSql.toString());
            }
            if (ObjectUtil.isNotEmpty(roleDataScopeUserIdList)){
                StringBuilder userSql = new StringBuilder();

                if (roleDataScopeUserIdList.size() == 1){
                    userSql.append("generator_userid").append(" = \"").append(roleDataScopeUserIdList.get(0)).append("\"");
                }else{
                    userSql.append("generator_userid").append(" in(");
                    roleDataScopeUserIdList = roleDataScopeUserIdList.stream().map(userId -> "\"" + userId + "\"").collect(Collectors.toList());
                    userSql.append(String.join(",",roleDataScopeUserIdList)).append(")");
                }

                sqlList.add(userSql.toString());
            }

            sqlFilter.append("(").append(String.join(" or ",sqlList)).append(")");
        }
        return sqlFilter.toString();
    }
}
